<%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/tlds/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/tlds/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/tlds/struts-menu.tld" prefix="menu" %>
<%@ taglib uri="/WEB-INF/tlds/struts-menu-el.tld" prefix="menu-el" %>
<%@ taglib uri="/WEB-INF/tlds/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/tlds/fmt.tld" prefix="fmt" %>
<%@ taglib uri="/WEB-INF/tlds/authz.tld" prefix="authz" %>
<%@ taglib uri="/WEB-INF/tlds/sitemesh-decorator.tld" prefix="decorator"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" scope="request"/>
<script type="text/javascript" src="${ctx}/dwr/interface/AttributeServiceDWR.js"></script>
<script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
<script type="text/javascript" src="${ctx}/dwr/util.js"></script>
<script type="text/javascript">
<!--
	function saveAttr() {
		var name = trim($F("name"));
		var oldName = $F("oldName");
		var attr = trim($F("attributeLDAP"));
		var typeVal=checkValue($('attributeForm'),'inputOutput');
		var id = $F("id");
		var namelist = $("attrNameList");	 
		var valuelist = $("attrValueList");	 
		var ignoreNull = $F("ignoreNull");
		var rule = trim($F("rule"));
		specName = unAllowedChars(name,'<bean:message key="allowedCharacters"/>');
		specAttr = unAllowedChars(attr,'<bean:message key="allowedCharacters"/>');
		if(name==null || name.length == 0) {
			alert('<bean:message key="error.nameFieldEmpty" />'); 
		} else if(attr==null || attr.length == 0) {
			alert('<bean:message key="error.attrFieldEmpty" />'); 
		} else if(specName ){
			alert('<bean:message key="error.nameContainsSpecialCharacter" />'); 
		} else if(specAttr) {		
			alert('<bean:message key="error.attrContainsSpecialCharacter" />'); 			
		}  else if((id==null || id.length==0) && contains(namelist,name)) {
			alert('<bean:message key="error.nameExistsInList"/>');
		}  else if(id!=null && id.length>0 && containsExcepted(namelist,name,oldName)) {
			alert('<bean:message key="error.nameExistsInList"/>');
		}  else if((id==null || id.length==0) && contains(valuelist,attr)) {
			alert('<bean:message key="error.attrExistsInList"/>');
		} else if(!attr || attr.length == 0) {
			alert('<bean:message key="error.ldapattrFieldEmpty" />'); 
		} else if(!typeVal || typeVal.length==0) {
			alert('<bean:message key="info.selectInputOutput" />');
		} else if(ignoreNull == 'on' && rule.length == 0) {
			alert('<bean:message key="info.ignoreNullCheckedWithoutRule" />');
		} else {
			attribute = {id:"", name:"", attributeLDAP:"", inputOutput:"", rule:"", ignoreNull:""};
			DWRUtil.getValues(attribute);
			attribute['inputOutput']=typeVal;
			AttributeServiceDWR.save(refreshAndClose,attribute);
		}
	}
	function refreshAndClose() {
		this.opener.location = '${ctx}/application.do?method=list';
		closePopup();
	}

// -->					
</script>
<div id="subcontent">
	<html:form styleId="attributeForm" action="/attribute" focus="name">
		<input type="hidden" id="method" name="method" value=""/>
		<input type="hidden" name="forward" value="resources" />
		<html:hidden  name="attributeForm" property="attribute.name" styleId="oldName"/>
		<html:hidden  name="attributeForm" property="attribute.id" styleId="id"/>
<authz:authorize ifAllGranted="ROLE_SUPERVISOR">	
		<p><bean:message key="attributePage.name" /><html:text styleId="name" property="attribute.name" maxlength="127"/></p>
		<p><bean:message key="attributePage.attribute" />
		<logic:equal name="attributeForm" property="attribute.id" value="">
			<html:text styleId="attributeLDAP" property="attribute.attributeLDAP" maxlength="127"/>
		</logic:equal>
		<logic:notEqual name="attributeForm" property="attribute.id" value="">
			<html:text styleId="attributeLDAP" property="attribute.attributeLDAP" maxlength="127"  disabled="true"/>
		</logic:notEqual>
		</p>
		<p>
			<html:radio styleId="inputOutput" property="attribute.inputOutput" value="0"/><bean:message key="attributePage.input" />
			<html:radio styleId="inputOutput" property="attribute.inputOutput" value="1"/><bean:message key="attributePage.output" />
			<html:radio styleId="inputOutput" property="attribute.inputOutput" value="2"/><bean:message key="attributePage.inputOutput" />
		</p>
		<p><bean:message key="attributePage.rule" /> <html:text styleId="rule"  property="attribute.rule" maxlength="127"/></p>
		<p><bean:message key="attributePage.apply" /><html:checkbox styleId="ignoreNull" property="attribute.ignoreNull" onclick="selectIgnore();"/></p>
		<p>
			<input name="validateAttrButton" type="submit" onclick="saveAttr();return false;" value="<bean:message key='attributePage.validate' />"/>&nbsp;
			<input name="cancelAttrButton" type="submit" onclick="closePopup();" value="<bean:message key='attributePage.cancel' />"/>
		</p>
		<div style="visibility: hidden">	
			<select name="attrNameList" id="attrNameList">
				<c:forEach var="attribute" items="${attributes}">
					<option value=""><c:out value="${attribute.name}" /></option>
				</c:forEach>
			</select>
			<select name="attrValueList" id="attrValueList">
				<c:forEach var="attribute" items="${attributes}">
					<option value=""><c:out value="${attribute.attributeLDAP}" /></option>
				</c:forEach>
			</select>
		</div>
</authz:authorize>
<authz:authorize ifAllGranted="ROLE_USER">				
		<p><bean:message key="attributePage.name" /><bean:write name="attributeForm" property="attribute.name" /></p>
		<html:hidden name="attributeForm" property="attribute.name" styleId="name"/>
		<p><bean:message key="attributePage.attribute" /><bean:write name="attributeForm" property="attribute.attributeLDAP" /></p>
		<p>
			<html:radio styleId="inputOutput" property="attribute.inputOutput" value="0" disabled="true"/><bean:message key="attributePage.input" />
			<html:radio styleId="inputOutput" property="attribute.inputOutput" value="1" disabled="true"/><bean:message key="attributePage.output" />
			<html:radio styleId="inputOutput" property="attribute.inputOutput" value="2" disabled="true"/><bean:message key="attributePage.inputOutput" />
		</p>
		<p><bean:message key="attributePage.rule" /><bean:write name="attributeForm" property="attribute.rule"/></p>
		<p><bean:message key="attributePage.apply" /><html:checkbox styleId="ignoreNull" property="attribute.ignoreNull" disabled="true" onclick="selectIgnore();"/></p>
		<p>
			<input name="closeAttrButton" type="submit" onclick="closePopup();" value="<bean:message key='attributePage.close' />"/>&nbsp;
		</p>

</authz:authorize>
	</html:form>
</div>
